package com.kust.smarthome.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kust.smarthome.web.imservice.Imservice;
import com.kust.smarthome.web.mapper.NoticeMapper;
import com.kust.smarthome.web.mapper.PersonDetailsMapper;
import com.kust.smarthome.web.mapper.PositionMapper;
import com.kust.smarthome.web.mapper.WatchMapper;
import com.kust.smarthome.web.model.Notice;
import com.kust.smarthome.web.model.PersonDetails;
import com.kust.smarthome.web.model.Position;
import com.kust.smarthome.web.model.Watch;
import com.kust.smarthome.web.service.IPersonDetailsService;
import com.kust.smarthome.web.utils.DistanceUtils;
import com.kust.smarthome.web.vo.PersonDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-06
 */
@Service
public class PersonDetailsServiceImpl extends ServiceImpl<PersonDetailsMapper, PersonDetails> implements IPersonDetailsService {

    @Autowired
    private PersonDetailsMapper personDetailsMapper;

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private WatchMapper watchMapper;

    @Resource
    private NoticeMapper noticeMapper;

    @Resource
    private Imservice imservice;

    @Resource
    private DistanceUtils distanceUtils;

    @Override
    public PersonDetails getPersonDetails(Integer userId, Integer personId) {
        QueryWrapper<PersonDetails> query = new QueryWrapper<>();
        query.eq("user_id", userId)
                .eq("person_id", personId);
        return personDetailsMapper.selectOne(query);
    }

    @Override
    public void insert(PersonDetails personDetails) {
        personDetailsMapper.insert(personDetails);
    }

    @Override
    public List<PersonDetails> personDetailByUser(Integer id) {
        QueryWrapper<PersonDetails> query = new QueryWrapper<>();
        query.eq("user_id", id);
        System.out.println(id);
        return personDetailsMapper.selectList(query);
    }

    @Override
    public boolean updatePersonDetails(PersonDetailsVo personDetailsVo) throws IOException {
        PersonDetails personDetails = personDetailsMapper.selectById(personDetailsVo.getId());
        if (personDetails == null) {
            return false;
        }

        String position = "longitude:" + personDetailsVo.getLongitude()
                + ";latitude:" + personDetailsVo.getLatitude() + ";";

        personDetails.setRateNow(personDetailsVo.getRateNow())
                .setTemp(personDetailsVo.getTemp())
                .setSteps(personDetailsVo.getSteps())
                .setPosition(position);
        //更新
        if (personDetailsMapper.updateById(personDetails) != 1) {
            log.error("personDetails更新失败");
            return false;
        }

        QueryWrapper<Position> query = new QueryWrapper<>();
        query.eq("user_id", personDetails.getUserId());
        Position positionD = positionMapper.selectOne(query);
//        double longitude1, double latitude1, double longitude2, double latitude2
        Double d = distanceUtils.getDistance(positionD.getLongitude(), positionD.getLatitude(), personDetailsVo.getLongitude(), personDetailsVo.getLatitude());
        if (d >= personDetails.getSafeDistance()/1000) {

            Watch watch = watchMapper.selectOne(new QueryWrapper<Watch>().eq("person_id", personDetailsVo.getId()));

            Notice notice = new Notice()
                    .setTypeId(8)
                    .setDeviceId(watch.getId())
                    .setCreated(LocalDateTime.now())
                    .setContent(personDetails.getPersonName() + "超出了安全距离!")
                    .setUserId(watch.getUserId())
                    .setState(1);
            noticeMapper.insert(notice);

            imservice.sendmessage(watch.getId().toString(),
                    "{\"msgtype\":\"NOTICE\",\"noticeId\":" + notice.getId() + "}");
        }
        return true;
    }
}
